package com.multithreading.chart2.Unsafe;

/**
 * @author chenqb
 * @project_name: concurrent_programming
 * @package_name: com.cqb.Unsafe
 * @ClassName: InstructionReordering
 * @description: 指令重排序
 * Author by cqb, Date on 2020/10/2，14:47
 * PS: Not easy to write code, please indicate.
 */
public class InstructionReordering {
    private static int num=0;
//   private volatile static boolean ready=false;
    private static boolean ready=false;
    public static class ReadThread extends Thread{
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()){
                if (ready){//step:1
                    System.out.println(num+num);//step:2
                }
                System.out.println("read thread...");
            }
        }
    }

    public static class Writethread extends Thread{
        @Override
        public void run() {
            num=2;//step:3
//            System.out.println(num);
            ready=true;//step:4
            System.out.println("writeThread set over...");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReadThread rt=new ReadThread();
        rt.start();
        Writethread wr=new Writethread();
        wr.start();
        Thread.sleep(10);
        rt.interrupt();
        System.out.println("main exit");
    }
}
